var app = new Vue({
    el: "#app",
    data: {
        // 页面中需要使用到的数据，键值对
        HOST: HOST,
        user_id: localStorage.user_id || sessionStorage.user_id,
        token: localStorage.token || sessionStorage.token,
        Problem_id: '',
        problem_data: '',
        problem_label: '',
        answer: '',
        answerson: '',
        show_data: false,
        show_son_data: true,
        // id:'',
        content: '',
        text_content: '',  //input框内容
        answer_name: '',

        problem_like: '',
        awesome: true,
        Add_status: '',
        num: null,
        sim_pros: '',

    },
    mounted: function () {
        // 一加载就需要做的，直接是代码
        this.Problem_id = get_query_string("id", 1);
        this.obtain_details();
        this.obtain_answer();
        this.obtain_laike();
        console.log(this.user_id);
    },
    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        obtain_post_laike: function () {
            axios.post(HOST + '/discussion/like/', {
                params: {
                    'problem_id': this.Problem_id,
                    'user_id': this.user_id,
                }
            }).then(response => {
                this.awesome = true
                location.reload()
            })
        },

        obtain_laike: function () {
            axios.get(HOST + '/discussion/like/', {
                params: {
                    'problem_id': this.Problem_id,
                    'user_id': this.user_id,
                }
            })
                .then(response => {
                    this.awesome = false
                })
                .catch(error => {
                    this.obtain_post_laike()

                })
        },


        obtain_details: function () {
            // console.log(Problem_id)
            axios.get(HOST + '/discussion/' + this.Problem_id + '/details/')
                .then(response => {
                    this.problem_data = response.data;
                    this.num = response.data.awesome;
                    this.problem_label = response.data.label_set;
                    if (this.user_id) {
                        axios.post(this.HOST+'/browse_histories/', {
                            history_title: this.problem_data.title,
                            history_address: window.location.href
                        }, {
                            headers: {
                                'Authorization': 'JWT ' + this.token
                            }
                        })
                    }
                });
        },

        obtain_answer: function () {
            axios.get(HOST + '/discussion/' + this.Problem_id + '/answer/')
                .then(response => {
                    console.log(this.Problem_id)
                    this.answer = response.data;
                    this.answerson = response.data.childanswer_set;
                    this.sim_pros = response.data[2].sim_pros

                })
        },

        hidden_inp: function () {
            if (this.show_data == false) {
                this.show_data = true;
                this.text_content = '';
            } else {
                this.text_content = '';
            }
        },
        edit: function () {
            if (this.show_data == true) {
                this.text_content = this.problem_data.content;
            } else {
                this.show_data = true;
                this.text_content = this.problem_data.content;

            }
        },
        cancel: function () {
            this.show_data = false
            this.text_content = '';
        },
        submit: function () {
            // console.log(this.Problem_id);
            if ((this.text_content).length > 0) {
                axios.post(HOST + '/discussion/' + this.Problem_id + '/answer/',
                    {
                        content: this.text_content
                    })
                    .then(response => {
                        this.obtain_answer();
                        this.cancel();
                    })
            } else {
                console.log('数据胃口');
            }
        },
        son_text: function (id) {
            console.log(id);
            layer.prompt({title: '随便写点啥，并确认', formType: 2}, function (text, index) {
                layer.close(index);
                // shangchuan
                axios.post(HOST + '/discussion/answer_son/', {


                    answer: id,
                    child_author: 1,
                    child_content: text
                })
                    .then(response => {
                        window.location.reload()
                    });
            });
        },

        Confirm: function () {
            if (this.awesome == true) {
                this.num += 1;
                axios.put(HOST + '/discussion/updata/', {}, {
                    params: {
                        problem_id: this.Problem_id, num: this.num
                    }
                })
                    .then(response => {
                        this.awesome = false;
                    });

            }
        },

        Cancel_Confirm: function () {
            if (this.awesome == false) {
                this.num -= 1;
                this.awesome = true;
                axios.put(HOST + '/discussion/updata/', {}, {
                    params: {
                        problem_id: this.Problem_id, num: this.num
                    }
                })
                    .then(response => {
                        this.awesome = true;
                    });
                axios.delete(HOST + '/discussion/updata/', {},{
                    params: {
                        problem_id: this.Problem_id, num: this.num
                    }
                })
                    .then(response => {
                        this.awesome =false;
                    })

            }
        }

    },

});